package com.hdd.biz.painting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdd.biz.painting.dto.TopicListRequest;
import com.hdd.biz.painting.dto.TopickRequest;
import com.hdd.biz.painting.entity.PaintingTopic;
import com.hdd.biz.painting.entity.PaintingTopicLink;
import com.hdd.biz.painting.mapper.PaintingTopicLinkMapper;
import com.hdd.biz.painting.mapper.PaintingTopicMapper;
import com.hdd.biz.painting.service.PaintingTopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdd.dto.SuperResponse;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 话题列表 服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-02-25
 */
@Service
public class PaintingTopicServiceImpl extends ServiceImpl<PaintingTopicMapper, PaintingTopic> implements PaintingTopicService {

    @Autowired
    private PaintingTopicLinkMapper topicLinkMapper;

    @Override
    public SuperResponse list(TopicListRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<PaintingTopic> wrapper = new QueryWrapper();
        Page<PaintingTopic> pages = new Page<>(request.getPageNo(),request.getPageSize());
        if(!StringUtil.isNullOrEmpty(request.getName())){
            wrapper.like("name",request.getName().trim());
        }
        wrapper.orderByDesc("status");
        wrapper.orderByDesc("prior");
        wrapper.orderByDesc("sort_no");
        IPage<PaintingTopic> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<PaintingTopic> agents = mapIPage.getRecords();
        response.setItems(agents);
        response.setTotals(pages.getTotal());
        return response;
    }

    @Override
    public SuperResponse add(TopickRequest request) {
        SuperResponse response = new SuperResponse();

        QueryWrapper<PaintingTopic> wrapper = new QueryWrapper<>();
        wrapper.eq("name",request.getName());
        int row =  baseMapper.selectCount(wrapper);
        if(row>0){
            response.setCode(-1000);
            response.setMessage("话题名称不允许重复");
            return response;
        }


        PaintingTopic topic = new PaintingTopic();
        topic.setName(request.getName());
        topic.setPictureUrl(request.getPictureUrl());
        topic.setStatus(request.getStatus());
        topic.setPrior(request.getPrior());
        topic.setSortNo(request.getSortNo());
        topic.setRemarks(request.getRemarks());
        topic.setColor(request.getColor());

        baseMapper.insert(topic);
        return response;
    }


    @Override
    public SuperResponse delete(TopickRequest request) {
        SuperResponse response = new SuperResponse();
        baseMapper.deleteById(request.getId());

        QueryWrapper<PaintingTopicLink> wrapper = new QueryWrapper<>();
        wrapper.eq("topic_id",request.getId());
        topicLinkMapper.delete(wrapper);

        return response;
    }


    @Override
    public SuperResponse update(TopickRequest request) {
        SuperResponse response = new SuperResponse();
        PaintingTopic topic = new PaintingTopic();
        topic.setName(request.getName());

        QueryWrapper<PaintingTopic> wrapper = new QueryWrapper<>();
        wrapper.eq("name",request.getName());
        wrapper.ne("id",request.getId());
        if(baseMapper.selectCount(wrapper)>0){
            response.setCode(-1000);
            response.setMessage("已存在相同话题!");
            return response;
        }

        topic.setId(request.getId());
        topic.setPictureUrl(request.getPictureUrl());
        topic.setStatus(request.getStatus());
        if("F".equals(request.getStatus())){
            topic.setSortNo(0);
        }
        topic.setPrior(request.getPrior());
        if("F".equals(request.getPrior())){
            topic.setSortNo(0);
        }
        topic.setSortNo(request.getSortNo());
        topic.setRemarks(request.getRemarks());
        topic.setColor(request.getColor());

        baseMapper.updateById(topic);
        return response;
    }


    @Override
    public SuperResponse info(TopickRequest request) {
        SuperResponse response = new SuperResponse();
        response.setItem(baseMapper.selectById(request.getId()));
        return response;
    }


    @Override
    public PaintingTopic findTopicByName(String name) {
        QueryWrapper<PaintingTopic> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public PaintingTopic inset(String name, String userId) {
        PaintingTopic topic = new PaintingTopic();
        topic.setName(name);
        topic.setUserId(userId);
        topic.setStatus("T");
        topic.setSortNo(0);
        baseMapper.insert(topic);

        return topic;
    }
}
